package com.example.cloud.controller.finance;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.cloud.constants.StateConstants;
import com.example.cloud.controller.common.BaseController;
import com.example.cloud.dao.finance.AccountChangeDetailsRepository;
import com.example.cloud.dao.finance.ConsumeDetailsRecordRepository;
import com.example.cloud.dao.finance.ConsumeRecordRepository;
import com.example.cloud.dao.finance.PaymentDetailsRepository;
import com.example.cloud.model.dto.QueryModel;
import com.example.cloud.model.dto.Result;
import com.example.cloud.model.finance.AccountChangeDetails;
import com.example.cloud.model.finance.ConsumeDetailsRecord;
import com.example.cloud.model.finance.ConsumeRecord;
import com.example.cloud.model.finance.PaymentDetails;
import com.example.cloud.utils.ResultUtil;
import org.apache.commons.lang.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created by wangtao on 2018/7/31.
 */
@RestController
@RequestMapping("/consume/record")
public class ConsumeRecordController extends BaseController {

    @Autowired
    private ConsumeRecordRepository consumeRecordRepository;

    @Autowired
    private ConsumeDetailsRecordRepository consumeDetailsRecordRepository;

    @Autowired
    private AccountChangeDetailsRepository accountChangeDetailsRepository;

    @Autowired
    private PaymentDetailsRepository paymentDetailsRepository;

    @PostMapping("/search")
    public Result search(@RequestBody QueryModel model) {
        return this.singleQuery(model, ConsumeRecord.class);
    }

    @GetMapping("/getTask/{id}")
    public Result getTask(@PathVariable Long id) {
        JSONObject json = new JSONObject();
        ConsumeRecord consumeRecord = consumeRecordRepository.findOne(id);
        AccountChangeDetails changeDetails = accountChangeDetailsRepository.findByRecordIdAndRecordType(id, StateConstants.RECORD_TYPE_CONSUME);
        PaymentDetails paymentDetails = paymentDetailsRepository.findByRecordIdAndRecordType(id, StateConstants.RECORD_TYPE_CONSUME);
        List<ConsumeDetailsRecord> details = consumeDetailsRecordRepository.findByRecordId(id);
        JSONArray ja = new JSONArray();
        for (ConsumeDetailsRecord d : details) {
            JSONObject jo = new JSONObject();
            jo.put("itemName", d.getItemName());
            jo.put("totalPrice", d.getPayAmount());
            jo.put("actualPayAmount", d.getActualAmount());
            jo.put("discountRate", d.getDiscount().equals(0d) ? null : d.getDiscount());
            jo.put("quantity", d.getQuantity());
            ja.add(jo);
        }
        json.put("details", ja);
        json.put("medicalNumber", consumeRecord.getMemberNumber());
        json.put("memberName", consumeRecord.getMemberName());
        json.put("afterLevelName", changeDetails.getAfterLevelName() == null ? "普通会员" : changeDetails.getAfterLevelName());
        json.put("payType", StateConstants.PRINT_PAY_TYPE_PAYMENT);
        json.put("afterStoreAccount", changeDetails.getAfterStoreAccount());
        json.put("afterGiftAccount", changeDetails.getAfterGiftAccount());
        json.put("afterTreatAccount", changeDetails.getAfterCourseAccount());
        json.put("actualPayAmount", consumeRecord.getActualAmount());
        json.put("storedAccount", changeDetails.getAfterStoreAccount());
        json.put("giftAccount", changeDetails.getAfterGiftAccount());
        json.put("bankCard", paymentDetails.getBankCard());
        json.put("alipay", paymentDetails.getAlipay());
        json.put("wechat", paymentDetails.getWechat());
        json.put("cash", paymentDetails.getCash());
        json.put("creationTime", DateFormatUtils.format(consumeRecord.getCreationTime(), "yyyy-MM-dd HH:mm:ss"));
        json.put("createdBy", consumeRecord.getCreatedBy());
        return ResultUtil.success(json);
    }
}
